package com.v9.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.dto.IdDto;
import com.v9.common.dto.PageInfoDto;
import com.v9.common.exception.BizException;
import com.v9.common.web.AbstractController;
import com.v9.system.domain.RoleInfo;
import com.v9.system.domain.RoleMenu;
import com.v9.system.domain.UserRole;
import com.v9.system.mapper.RoleDeptMapper;
import com.v9.system.mapper.RoleMenuMapper;
import com.v9.system.mapper.UserRoleMapper;
import com.v9.system.service.DeptInfoService;
import com.v9.system.service.RoleInfoService;
import com.v9.system.service.UserInfoService;
import lombok.Data;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping(
        value = {"/system/role"},
        method = {RequestMethod.POST}
)
public class RoleController extends AbstractController {
    @Resource
    private RoleInfoService roleInfoService;
    @Resource
    private DeptInfoService deptInfoService;
    @Resource
    private RoleDeptMapper roleDeptMapper;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @RequestMapping("/list")
    public Page<RoleInfo> list(@RequestBody PageInfoDto dto) {
        return roleInfoService.queryByPage(new RoleInfo(), dto.getPageable());
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @RequestMapping(value = "/view/")
    public RoleInfoDto view(@RequestBody IdDto dto) {
        RoleInfo byId = roleInfoService.findById(dto.getId());
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", dto.getId());
        List<RoleMenu> sysRoleMenus = roleMenuMapper.selectList(queryWrapper);
        RoleInfoDto result = clone(RoleInfoDto.class, byId);
        List<Long> collect = sysRoleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        result.setMenuIds(collect.toArray(new Long[collect.size()]));
        return result;
    }


    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @RequestMapping("/save")
    public void save(@RequestBody RoleInfoDto info) {
        RoleInfo clone = clone(RoleInfo.class, info);
        roleInfoService.save(clone, info.getMenuIds());
    }

    @Data
    public static class RoleInfoDto {
        private Long id;
        private String name;
        private String code;
        private Integer seq;
        private Boolean status;
        private Long roleTypeId;
        private Long[] menuIds;
    }

    @Data
    public static class RoleInfoScopeDto {
        private Long id;
        private Boolean deptCheckStrictly;
        private Long[] deptIds;
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @RequestMapping("/changeStatus")
    public void changeStatus(@RequestBody ChangeRoleStatusDto dto) {
        roleInfoService.save(dto.getId(), "status", dto.getStatus());
    }

    @Data
    public static class ChangeRoleStatusDto {
        private Long id;
        private Boolean status;
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @RequestMapping("/remove")
    public void remove(@RequestBody IdDto dto) {
        QueryWrapper<UserRole> query = new QueryWrapper<>();
        query.eq("role_id", dto.getId());
        Long aLong = userRoleMapper.selectCount(query);
        if (aLong.longValue() > 0L) {
            throw new BizException("不能删除存在用户的角色");
        }
        roleInfoService.remove(dto.getId());
    }

}
